package org.example.bookshelf_manager.service;

import org.example.bookshelf_manager.enums.BookStatus;
import org.example.bookshelf_manager.mapper.BookMapper;
import org.example.bookshelf_manager.model.BookInfo;
import org.example.bookshelf_manager.model.PageRequest;
import org.example.bookshelf_manager.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class BookService {



    @Autowired
    private BookMapper bookMapper;

//    public List<BookInfo> getBookList(){
////        1.从dao层拿到数据 进行业务逻辑处理
////        BookDao bookDao = new BookDao();
//        List<BookInfo> bookInfos = bookDao.mockData();
//        for(BookInfo bookInfo:bookInfos){
//            if (bookInfo.getStatus() == 0){
//                bookInfo.setStatusCN("不可借阅");
//            } else {
//                bookInfo.setStatusCN("可借阅");
//            }
//        }
//        return bookInfos;
//    }

    public Integer insertBook(BookInfo bookInfo) {
        return bookMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest){
//        1. 获取总记录数
        Integer count = bookMapper.count();

//        2. 获取 当前页的记录
        List<BookInfo> bookInfoList =  bookMapper.queryBookByPage(pageRequest.getOffset(), pageRequest.getPageSize());

//        3. 处理 可借阅 不可借阅 状态
     for (BookInfo bookInfo : bookInfoList){
        bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
     }

        return new PageResult<BookInfo>(bookInfoList,count,pageRequest);
    }

//    根据 Id 查看图书详情
    public BookInfo queryBookById(Integer bookId) {
        BookInfo bookInfo =  bookMapper.queryBookById(bookId);
        bookInfo.setStatusCN(BookStatus.getDescByCode(bookInfo.getStatus()).getDesc());
        return bookInfo;
    }

    public Integer updateBookById(BookInfo bookInfo) {
        return bookMapper.updateBookById(bookInfo);
    }

    public Integer deleteBook (Integer id){
        return bookMapper.deleteBook(id);
    }

    public Integer batchDeleteBook(List<Integer> ids){
        return bookMapper.batchDeleteBook(ids);
    }
}
